$(function(){
    Util.init();
});

var Util = {
    init: function() {},

    getCookie: function(name) {
        var aCookie = document.cookie.split(';');

        for(var i=0; i<aCookie.length; i++) {
            if ( aCookie[i].indexOf(name + '=') > -1 )  {
                 var splitData = aCookie[i].split('=');
                 return unescape(splitData[1]);
            }
        }
        return null;
    },


    getCookArr: function(key,arrayName) {
        var aCookie = document.cookie.split('; ');
        var ret = '';
        for (var i=0; i < aCookie.length; i++) {
            if(!arrayName) {
                if(aCookie[i].substring(0, key.length+1) == key+'=') {
                    ret = unescape(aCookie[i].substring(key.length+1));
                    break;
                }
            } else {
                if(aCookie[i].substring(0, arrayName.length+1) == arrayName+'=') {
                    var arCook = unescape(aCookie[i].substring(arrayName.length+1)).split('&');
                    for( var j=0; j < arCook.length; j++) {
                        if(arCook[j].substring(0, key.length+1) == key+'=') {
                            ret = unescape(arCook[j].substring(key.length+1));
                            break;
                        }
                    }
                }
            }
        }
        if(!ret) ret = '';
        return ret;
    },

    /**
     * 쿠키 등록
     */
    setCookie: function(name, value) {
        var argc = Util.setCookie.arguments.length;
        var argv = Util.setCookie.arguments;
        var expires = (argc > 2) ? argv[2] : 0;
        var path = (argc > 3) ? argv[3] : null;
        var domain = (argc > 4) ? argv[4] : null;
        var secure = (argc > 5) ? argv[5] : false;

        if(expires != 0) {
            var dd = new Date(expires * 1000);
            var expst = '; expires='+dd.toGMTString();
        } else expst = '';

        var cookstr = name + '=' + value +
            ((expires == 0) ? '' : (expst)) +
            ((path == null) ? '' : ('; path=' + path)) +
            ((domain == null) ? '' : ('; domain=' + domain)) +
            ((secure == true) ? '; secure' : '');
        document.cookie = cookstr;
    },

    setCookArr : function(key,value,arrayName,until) {
        var cookStr = '';
        var findArr = false;
        var findKey = false;
        var cnt = 0;

        var aCookie = document.cookie.split('; ');
        if(!until) until = 0;
        if( !arrayName ) { this.setCookie(key,value,until,'/'); return; }

        for (var i=0; i < aCookie.length; i++) {
            if(aCookie[i].substring(0, arrayName.length+1) != arrayName+'=') continue;

            cookStr = '';
            findArr = true;

            var arCook = aCookie[i].substring(arrayName.length+1).split('&');
            for( var j=0; j<arCook.length; j++) {
                if(arCook[j] == '') continue;

                if(arCook[j].substring(0, key.length+1) == key+'=') {
                    findKey = true;
                    if( cnt == 0 ) cookStr = (key + '=' + value);
                    else cookStr += ('&' + key + '=' + value);
                } else {
                    if(cnt == 0) cookStr = arCook[j];
                    else cookStr += ('&' + arCook[j]);
                }
                cnt++;
            }

            if( !findKey ) {
                if(cnt == 0) cookStr = (key + '=' + value);
                else cookStr += ('&' + key + '=' + value);
            }
        }

        if(!findArr) cookStr = (key + '=' + value);

        this.setCookie(arrayName,cookStr,until,'/');
    },

    lengthCheck: function (data) {
        var count = 0;
        for ( var i=0; i < data.length; i++ ) {
            if( data.charCodeAt(i) < 127 )
                count++;
            else
                count = count + 2;
        }
        return count;
    },

    cutString: function(data,len) {
        var res = '';
        var count = 0;
        for( var i=0; i < data.length; i++ ) {
            count += (data.charCodeAt(i) > 128) ? 2 : 1;
            if(count > len) return data.substr(0,i);
        }

        return data;
    },


    // 문자열의 넓이를 픽셀 단위로 계산
    getStringPixelWidth: function (v){
        var asciiCode;
        var vLength = v.length;
        var character;
        var characterWidth;
        var totalWidth = 0;

        var specialCharZize = 6;
        var multibyteCharSize = 12;
        var baseCharStart = 32;
        var baseCharEnd =  127;
        var asciiCharSize = Array(4,4,4,6,6,10,8,4,5,5,6,6,4,6,4,6,6,6,6,6,6,6,6,6,6,6,4,4,8,6,8,6,12,8,8,9,8,8,7,9,8,3,6,8,7,11,9,9,8,9,8,8,8,8,8,10,8,8,8,6,11,6,6,6,4,7,7,7,7,7,3,7,7,3,3,6,3,11,7,7,7,7,4,7,3,7,6,10,7,7,7,6,6,6,9,6);

        for(i=0; i<vLength; i++){
            character = string_value.substring(i,(i+1));
            asciiCode = character.charCodeAt(0);

            if(asciiCode < baseCharStart){
                characterWidth = specialCharZize;
            }else if(asciiCode <= baseCharEnd){
                idx = asciiCode - baseCharStart;
                characterWidth = asciiCharSize[idx];
            }else if(asciiCode > baseCharEnd){
                characterWidth = multibyteCharSize;
            }
            totalWidth += characterWidth;
        }

        return totalWidth;
    },

    numberFormat: function ( number, decimals, dec_point, thousands_sep ) {
        var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;
        var d = dec_point == undefined ? '.' : dec_point;
        var t = thousands_sep == undefined ? ',' : thousands_sep, s = n < 0 ? '-' : '';
        var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + '', j = (j = i.length) > 3 ? j % 3 : 0;
        return s + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : '');
    },
    
    nl2br: function(str){ 
        return str.replace(/([^>])\n/g, '$1<br />\n');
    },

    //마우스오버시 색상 처리
    onColor: function (el, color) {
        el = $(el);
        el.setStyle({backgroundColor:color});
        el.onmouseout = function(){
            el.setStyle({backgroundColor:''});
        };
    },

    //랜덤값 생성
    rand: function(min, max){
        return Math.floor(Math.random() * (max - min + 1) + min);
    },
    
    isArray : function (obj) {
        return (obj.constructor.toString().indexOf("Array") == -1);
    },
    
    end: function() {}
};